package com.ruoyi.common.rabbitmq.configure;

import com.ruoyi.common.rabbitmq.constant.RabbitmqConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMQConfig {

    private final String dle = "x-dead-letter-exchange";
    private final String dlk = "x-dead-letter-routing-key";
    private final String ttl = "x-message-ttl";


    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(RabbitmqConstants.EXCHANGE,true,false);

    }

    @Bean
    public Queue SMSQueue(){
        return new Queue(RabbitmqConstants.SMS_NOTIFY_QUEUE,true);
    }

    @Bean
    public Binding SMSBindingBuilder(){
        return BindingBuilder.bind(SMSQueue()).to(directExchange()).with(RabbitmqConstants.SMS_ROUTINGKEY);
    }

    @Bean
    public Queue EmailQueue(){
        return new Queue(RabbitmqConstants.EMAIL_NOTIFY_QUEUE,true);
    }

    @Bean
    public Binding EmailBindingBuilder(){
        return BindingBuilder.bind(EmailQueue()).to(directExchange()).with(RabbitmqConstants.EMAIL_ROUTINGKEY);
    }

    @Bean
    public Queue wechatQueue(){
        return new Queue(RabbitmqConstants.WECHAT_NOTIFY_QUEUE,true);
    }

    @Bean
    public Binding WechatBindingBuilder(){
        return BindingBuilder.bind(wechatQueue()).to(directExchange()).with(RabbitmqConstants.WECHAT_ROUTINGKEY);
    }

    @Bean
    public Queue emailFileQueue(){
        return new Queue(RabbitmqConstants.EMAIL_FILE_QUEUE,true);
    }

    @Bean
    public Binding emailFileBindingBuilder(){
        return BindingBuilder.bind(emailFileQueue()).to(directExchange()).with(RabbitmqConstants.EMAIL_FILE_ROUTINGKEY);
    }

    /////////////////////////////////////////////
    @Bean
    public Queue faultEmailQueue(){
        return new Queue("fault.email.queue",true);
    }

    @Bean
    public Binding faultEmailBinding(){
        return BindingBuilder.bind(faultEmailQueue()).to(directExchange()).with("fault");
    }

    @Bean
    public Queue faultSMSQueue(){
        return new Queue("fault.sms.queue",true);
    }

    @Bean
    public Binding faultSMSBinding(){
        return BindingBuilder.bind(faultSMSQueue()).to(directExchange()).with("fault");
    }
    //////////////////////////////////////////////

    @Bean
    public TopicExchange dlTopicExchange(){
        return new TopicExchange(RabbitmqConstants.DL_TOPIC_EXCHANGE,true,false);
    }

    @Bean
    public Queue dlControlBoxUpQueue(){
        return new Queue(RabbitmqConstants.DL_CONTROL_BOX_UP_QUEUE,true);
    }

    @Bean
    public Binding BindingErrorQueueAndExchange(){
        return BindingBuilder.bind(dlControlBoxUpQueue()).to(dlTopicExchange()).with(RabbitmqConstants.DL_CONTROL_UP_ROUTING_KEY);
    }

    @Bean
    public Queue elevenExpireQueue(){
        Map<String,Object> params = new HashMap<String,Object>();
        //设置队列的过期时间
        params.put(ttl,660*1000);
        //声明当前队列绑定的死信交换机
        params.put(dle, RabbitmqConstants.DL_TOPIC_EXCHANGE);
        //声明当前队列的死信路由键
        params.put(dlk, RabbitmqConstants.DL_CONTROL_UP_ROUTING_KEY);
        return QueueBuilder.durable(RabbitmqConstants.ELEVEN_EXPIRE_QUEUE).withArguments(params).build();
    }

    @Bean
    public Binding topicBinding(){
        return BindingBuilder.bind(elevenExpireQueue()).to(dlTopicExchange()).with(RabbitmqConstants.DL_ELEVEN_ROUTING_KEY);
    }

//    @Bean
//    public Queue thirtyExpireQueue(){
//        Map<String,Object> params = new HashMap<String,Object>();
//        //设置队列的过期时间
//        params.put(ttl,1800*1000);
//        //声明当前队列绑定的死信交换机
//        params.put(dle,RabbitmqConstants.DL_TOPIC_EXCHANGE);
//        //声明当前队列的死信路由键
//        params.put(dlk,RabbitmqConstants.DL_CONTROL_UP_ROUTING_KEY);
//        return QueueBuilder.durable(RabbitmqConstants.THIRTY_EXPIRE_QUEUE).withArguments(params).build();
//    }



}
